﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;

public partial class ListTopics : System.Web.UI.Page
{
    protected PagedDataSource pds = new PagedDataSource();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    private void BindData()
    {
        CmsDataContext db = new CmsDataContext();
        var q = from aT in db.Topic
                select aT;

        #region 检索条件
        string search = SearchConditionHidden.Value.Trim();
        if (search != "")
        {
            Expression<Func<Topic, bool>> lmbda = PredicateBuilder<Topic>.CreatePredicate(
                new string[] { 
                    "Name", "Description"}, search, "aT");
            q = q.Where(lmbda);
        } 
        #endregion

        var p = q.OrderBy(aT => aT.Name).Select(aT => new
        {
            ID = aT.ID,
            Name = aT.Name,
            Description = aT.Description,
            Parent = aT.Parent.Name
        });
        
        pds = new PagedDataSource();
        pds.AllowPaging = true;
        pds.PageSize = Toolkit.PageSize;
        pds.DataSource = p.ToList();


        #region 获取当前要显示的页
        int pageIndex = 0;
        pageIndex = Convert.ToInt32(CurrentPage.Value) - 1;
        if (pageIndex >= pds.PageCount)
        {
            pageIndex = pds.PageCount - 1;
        }
        if (pageIndex <= 0)
        {
            pageIndex = 0;
        }
        #endregion

        pds.CurrentPageIndex = pageIndex;

        TopicsView.DataSource = pds;
        TopicsView.DataBind();

        CurrentPage.Value = Convert.ToString(pageIndex + 1);
        SearchCondition.Text = SearchConditionHidden.Value.Trim();
    }

    protected void DeleteTopic_Click(object sender, EventArgs e)
    {
        string tids = Request.Params["chkID"];

        if (tids != null)
        {
            string[] ids = tids.Split(',');
            CmsDataContext db = new CmsDataContext();

            #region 先将子栏目的父栏目清空
            var topics = from aT in db.Topic
                         where ids.Contains(aT.ParentID)
                         select aT;
            foreach (Topic t in topics)
            {
                t.ParentID = null;
            }
            #endregion

            #region 将文章的栏目信息清空
            var articles = from aA in db.Article
                           where ids.Contains(aA.TopicID)
                           select aA;
            foreach (Article a in articles)
            {
                a.TopicID = null;
            }
            #endregion

            #region 最后删除栏目
            topics = from aT in db.Topic
                     where ids.Contains(aT.ID)
                     select aT;
            db.Topic.DeleteAllOnSubmit(topics);
            #endregion

            //提交数据库更新
            db.SubmitChanges();

            MessageBox.Message = "所选栏目已删除。";
        }

        //重新绑定数据
        BindData();
    }

    protected void ChangePage(object sender, CommandEventArgs e)
    {
        string kind = Convert.ToString(e.CommandArgument);
        int p = Convert.ToInt32(CurrentPage.Value);

        if (kind == "F")
        {
            CurrentPage.Value = "1";
        }
        else if (kind == "P")
        {
            p--;
            CurrentPage.Value = Convert.ToString(p);
        }
        else if (kind == "N")
        {
            p++;
            CurrentPage.Value = Convert.ToString(p);
        }
        else if (kind == "L")
        {
            CurrentPage.Value = Convert.ToString(Int32.MaxValue);
        }
        else if (kind == "G")
        {
            TextBox gtp;
            foreach (Control c in TopicsView.Controls)
            {
                gtp = c.FindControl("GotoPage") as TextBox;
                if (gtp != null)
                {
                    try
                    {
                        CurrentPage.Value = Convert.ToString(Convert.ToInt32(gtp.Text.Trim()));
                    }
                    catch
                    {
                        //CurrentPage.Value = "1";
                    }

                    break;
                }
            }
        }

        BindData();
    }

    protected void Search_Click(object sender, EventArgs e)
    {
        SearchConditionHidden.Value = SearchCondition.Text.Trim();
        BindData();
    }
}